package algorithm_optimal_solution.LinkedList;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

//将单链表的每K个节点之间逆序：用栈，或者直接在链表上修改
public class LinkedListEveryKNodeReverse {

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void reverseNode(Node head,int number){
        int count = 0;
        Node node = head;
        Node temp = head;
        Node next = null;
        Node pre = null;
        Node tail = null;
        Node newHead = null;
        while(node != null){
            if(++count == number){
                if(tail != null){
                    tail.setNode(node);
                }else{
                    newHead = node;
                }
                node = node.getNode();
                tail = temp;
                while(count != 0){
                    next = temp.getNode();
                    temp.setNode(pre);
                    pre = temp;
                    temp = next;
                    count--;
                }
                tail.setNode(node);
            }else{
                if(count == 1) {
                    temp = node;
                }
                node = node.getNode();
            }
        }
        StringBuilder sb = new StringBuilder();
        while(newHead != null){
            sb.append(newHead.getValue()).append(" ");
            newHead = newHead.getNode();
        }
        System.out.println(sb);
    }

    public void reverseNodeByStack(Node head,int number){
        Stack<Node> stack = new Stack<>();
        Node node = head;
        Node next = null;
        Node pre = null;
        Node newHead = head;
        while(node != null){
            next = node.getNode();
            stack.push(node);
            if(stack.size() == number){
                Node cur = stack.pop();
                if(pre != null){
                    pre.setNode(cur);
                }
                while(!stack.isEmpty()){
                    cur.setNode(stack.pop());
                    cur = cur.getNode();
                }
                cur.setNode(next);
                pre = cur;
                newHead = newHead == head ? node : newHead;
            }
            node = next;
        }
        StringBuilder sb = new StringBuilder();
        while(newHead != null){
            sb.append(newHead.getValue()).append(" ");
            newHead = newHead.getNode();
        }
        System.out.println(sb);
    }

    public void reverseNodeNoStack(Node head,int number){
        Stack<Node> stack = new Stack<>();
        Node node = head;
        Node start = null;
        Node next = null;
        Node pre = null;
        int count = 0;
        while(node != null){
            next = node.getNode();
            if(++count == number){
                start = pre == null ? head : pre.getNode();
                head = pre == null ? node : head;
                reverse(pre,start,node,next);
                pre = start;
                count = 0;
            }
            node = next;
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.println(sb);
    }

    private void reverse(Node left,Node start,Node end,Node right){
        if(left != null){
            left.setNode(end);
        }
        Node pre = start;
        Node cur = start.getNode();
        Node next = null;
        while(cur != right){
            next = cur.getNode();
            cur.setNode(pre);
            pre = cur;
            cur = next;
        }
        start.setNode(right);
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        String[] numbers = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(numbers[i]),node);
        }
        int number = Integer.parseInt(scanner.readLine());
//        new LinkedListEveryKNodeReverse().reverseNode(node,number);
//        new LinkedListEveryKNodeReverse().reverseNodeByStack(node,number);
        new LinkedListEveryKNodeReverse().reverseNodeNoStack(node,number);
    }
}
